/**
 * Script for index.html
 */

/**
 * Start the js file.
 */
function init() {
   initSnacksTable();
}

function initSnacksTable() {
   var xmlDoc = loadXML("./data/snacksInfo.xml");

   if(!xmlDoc) {
      alert("Your browser's version is too lower, " +
         "please update to IE5.0 or higher");
      return;
   }

   var snacks = xmlDoc.getElementsByTagName("snacks");
   var snack = snacks[0].getElementsByTagName("snack");
   var rootDiv = $("orderContent");

   for(var i = 0; i < snack.length; i++) {
      var id = snack[i].getAttribute("id");
      var name = snack[i].getAttribute("name");
      var price = snack[i].getAttribute("price");
      var remarks = snack[i].getAttribute("remarks");
      var picUrl = "./pictures/" + snack[i].getAttribute("picUrl");
      var type = snack[i].getAttribute("type");
      var vDiv = document.createElement("div");
      vDiv.className = "snackView";
      var imgDiv = document.createElement("div");
      imgDiv.className = "snackImg";
      var npDiv = document.createElement("div");
      npDiv.className = "namePrice";
      var nDiv = document.createElement("div");
      nDiv.className = "nameDiv";
      var pDiv = document.createElement("div");
      pDiv.className = "priceDiv";
      var cDiv = document.createElement("div");
      cDiv.className = "snackCount";

      var img = document.createElement("img");
      img.src = picUrl;
      img.width = 100;
      img.height = 100;
      
      rootDiv.appendChild(vDiv);
      vDiv.appendChild(imgDiv);
      vDiv.appendChild(npDiv);
      vDiv.appendChild(cDiv);

      imgDiv.appendChild(img);
      npDiv.appendChild(nDiv);
      npDiv.appendChild(pDiv);
      nDiv.innerHTML = name;
      pDiv.innerHTML = price + "元";
      cDiv.innerHTML = "订<input type='text' size='1' value='0' name='" +
         encodeName("snack", id) + "'/>份";
   }

   var select = $("hour");
   var now = new Date();

   for(var i = now.getHours(); i < 16; i++) {
      var hoption = document.createElement("option");
      hoption.text = i;
      hoption.innerHTML = i;
      hoption.value = i;
      select.appendChild(hoption);
   }
}

function verifyOrder() {
   var form = $("orderForm");
   var elem;
   var orderedItem = 0;

   for(var i = 0; i < form.elements.length; i++) {
      elem = form.elements[i];
      elem.value = elem.value.trim();

      if(elem.type == "text" && elem.name.startsWith("snack")) {
         if(elem.value == null || elem.value == "") {
            elem.value = 0;
         }
         else if(isNaN(elem.value)) {
            alert("请认真填写订购数量");
            elem.focus();
            return false;
         }

         orderedItem = orderedItem + parseInt(elem.value, 10);
      }
      else if(elem.name == "uname") {
         if(elem.value == null || elem.value == "") {
            alert("Name can't be null");
            elem.focus()
            return false;
         }
      }
      else if(elem.name == "phone") {
         if(elem.value == null || elem.value == "") {
            alert("Phone can't be null");
            elem.focus();
            return false;
         }
         else if(!elem.value.isMobile() && !elem.value.isTel()) {
            alert("Your phone number is incorrected");
            elem.focus();
            return false;
         }
      }
      else if(elem.name == "address") {
         if(elem.value == null || elem.value == "") {
            alert("Address can't be null");
            elem.focus();
            return false;
         }
      }
      else if(elem.name == "request") {
         // @temp
      }
      else if(elem.name == "startTime") {
         elem.value = new Date().format("yyyy-MM-dd hh:mm:ss");
      }
      else if(elem.name == "willTime") {
         var now = new Date();
         elem.value = new Date(now.getFullYear(), now.getMonth(), now.getDate(),
            form["hour"].value, form["minute"].value, 0)
            .format("yyyy-MM-dd hh:mm:ss");
      }
      else if(elem.name == "status") {
         elem.value = 0; // started.
      }
   }

   if(orderedItem <= 0) {
      alert("You havn't ordered something!");
      return false;
   }

   var postData = form.serialize();
   var ajax = new Ajax.Request(
      "bin/orderSnack.php",
      {
         method: "post",
         postBody: postData,
         onSuccess: successHandler
      }
   );

   $("orderBtn").disabled = true;

   return true;
}

function successHandler(req) {
   var content = req.responseText;
   $("customerInfoDiv").className = "hidden";
   $("orderedInfoDiv").className = "";
   $("orderResult").innerHTML = content;
   $("orderBtn").disabled = false;
}

function orderButtonClicked() {
   var div = $("customerInfoDiv");

   if(div.className == "hidden") {
      div.className = "";
      $("orderedInfoDiv").className = "hidden";
      $("orderBtn").disabled = false;
   }
   else {
      div.className = "hidden";
   }
}

function reviewButtonClicked() {
   var div = $("orderedInfoDiv");

   if(div.className == "hidden") {
      div.className = "";
      $("customerInfoDiv").className = "hidden";
   }
   else {
      div.className = "hidden";
   }
}

function getOrder() {
   var postData = "op=getOrder&snackID=" + $("orderForm").sid.value;
   var ajax = new Ajax.Request(
      "bin/orderSnack.php",
      {
         method: "post",
         postBody: postData,
         onSuccess: successHandler
      }
   );
}

function encodeName(prefix, id) {
   return prefix + "!<" + id + ">";
}

function decodeName(id) {
   return id.split("!<")[1].split(">")[0];
}